<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Common;

use App\Exceptions\ApiException;
use App\Http\Controllers\WeChatApplyPay;
use App\Logic\WXOrderLogic;
use App\Logic\SmsLogic;
use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Tool
{

    public static $request;

    public static function jsonHandle($data = [], $code = 0, $desc = '')
    {
        $arr = ['ret' => $code, 'desc' => $desc];
        if ($data) {
            $arr['data'] = $data;
        }

//        $response = preg_replace_callback("#\\\u([0-9a-f]{4})#i", function ($m) {
//            return iconv('UCS-2BE', 'UTF-8', pack('H4', $m[1]));
//        }, json_encode($arr));
        $response = json_encode($arr, JSON_UNESCAPED_UNICODE);
        Logger::instance()->info(self::getUri(), "RESPONSE:  $response");
        return $response;
    }

    /**
     * 获取当前请求的路由名
     * @return mixed
     */
    public static function getUri()
    {
        if (empty(self::$request)) {
            return '空uri，没有继承base 控制器';
        }
        return self::$request->route()->uri;
    }


    public static function setRedisRoomKey($room_id, $pwd)
    {
        $path = file_get_contents(base_path() . '/redisscript/create_room.lua');
        return Redis::eval($path, 3, env('REDIS_ROOM_KEY'), $room_id, $pwd);
    }


    /**
     * 根据随机房间号获取房间ID
     * @param $rand_password
     * @return mixed
     */
    public static function getRedisRoomId($rand_password)
    {
        return Redis::eval(file_get_contents(base_path() . '/redisscript/get_room.lua'), 2, env('REDIS_ROOM_KEY'), $rand_password);
    }


    /**
     * 返回毫秒
     * @return float
     */
    public static function msectime()
    {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }


    /**
     * 创建订单数据
     * @param $order_id
     * @param $price
     * @param $uid
     * @param $pay_type
     * @param $good_type
     * @param $goods_id
     */
    public static function createPayInfo($order_id,$price, $uid, $pay_type, $good_type, $goods_id)
    {
         DB::table('t_pay_info')->insertGetId([
            'order_id' => $order_id,
            'pay_type' => $pay_type,
            'uid' => $uid,
            'price' => floatval($price),
            'order_status' => 0,
            'goods_id' => $goods_id,
            'goods_type' => $good_type
        ]);
    }


    public static function xmlToArray($xml)
    {
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }


    public static function skey()
    {
        return crypt(microtime(),mt_rand(10000,99999));
    }

}